package com.vanysoft.business.base.user.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.vanysoft.business.base.org.dao.BaseOrgMapper;
import com.vanysoft.business.base.org.entity.TBaseOrg;
import com.vanysoft.business.base.tag.controller.demo.UserExport;
import com.vanysoft.business.base.team.dao.BaseTeamUsersMapper;
import com.vanysoft.business.base.team.entity.TBaseTeamUsers;
import com.vanysoft.business.base.user.controller.demo.BaseUserDemo;
import com.vanysoft.business.base.user.controller.demo.ExcelOrgUserDemo;
import com.vanysoft.business.base.user.controller.demo.ExcelUserDemo;
import com.vanysoft.business.base.user.dao.BaseOrgUsersMapper;
import com.vanysoft.business.base.user.dao.BaseUserMapper;
import com.vanysoft.business.base.user.dao.BaseUserRolesMapper;
import com.vanysoft.business.base.user.dao.BaseUserTagsMapper;
import com.vanysoft.business.base.user.entity.TBaseOrgUsers;
import com.vanysoft.business.base.user.entity.TBaseUser;
import com.vanysoft.business.base.user.entity.TBaseUserRoles;
import com.vanysoft.business.base.user.entity.TBaseUserTags;
import com.vanysoft.business.index.dao.LoginMapper;
import com.vanysoft.business.index.entity.LoginUserinfo;
import com.vanysoft.common.utils.desc.DescUtil;
import com.vanysoft.common.utils.string.StringUtil;
import com.vanysoft.framework.config.file.FileProperties;
import com.vanysoft.framework.constants.DbConstants;
import com.vanysoft.framework.exception.VanySoftBaseException;
import com.vanysoft.framework.messagesource.MessageCodeConstant;
import com.vanysoft.framework.utils.CommonUtil;
import com.vanysoft.framework.utils.PinYin;
import com.vanysoft.framework.utils.file.Base64ImageUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 项目名称：SharpClaw移动门户
 * 类名称：BaseUserService
 * 类描述：人员管理
 * 创建人：SharpClaw_x
 * 创建时间：2017年5月24日 下午4:37:45
 * 修改人：SharpClaw_x
 * 修改时间：2017年5月24日 下午4:37:45
 * 版本信息：@version 1.0
 * Copyright (c) 2017SharpClaw-版权所有
 * 修改备注：
 */
@Service
@Transactional(readOnly = true)
public class BaseUserService {

    @Autowired
    private BaseUserMapper baseUserMapper;
    @Autowired
    private BaseOrgUsersMapper baseOrgUsersMapper;
    @Autowired
    private BaseTeamUsersMapper baseTeamUsersMapper;
    @Autowired
    private BaseUserTagsMapper baseUserTagsMapper;
    @Autowired
    private BaseUserRolesMapper tBaseUserRolesMapper;
    @Autowired
    private BaseOrgMapper baseOrgMapper;
    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private BaseOrgMapper tBaseOrgMapper;
    //	@Autowired
//	private UploadConfig uploadConfig;
    @Autowired
    private FileProperties fileProperties;//可扩展其他路径


    /**
     * Function Name               addbaseUser
     *
     * @param baseUserDemo
     * @throws VanySoftBaseException
     * @description 添加人员
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月19日 下午3:12:47           SharpClaw_x                    Initial
     * *********************************************************************
     */
    @Transactional
    public String addbaseUser(BaseUserDemo baseUserDemo, String orgList, String teamList, String tagList, String roleList) throws VanySoftBaseException {

        //检查userAccount是否重复
        if (false == checkUsercode(baseUserDemo.getUserAccount())) {
            throw new VanySoftBaseException(MessageCodeConstant.UserAndRoleException.RTN_CODE_USER_CODE);
        }

        TBaseUser userEntity = new TBaseUser();
        userEntity.setUserId(UUID.randomUUID().toString());
        userEntity.setUserAccount(baseUserDemo.getUserAccount().trim());
        userEntity.setUserName(baseUserDemo.getUserName());

        String userPassword = DescUtil.encrypt(DbConstants.INIT_PASSWORD + baseUserDemo.getUserAccount() + "_" + baseUserDemo.getUserAccount().length());
        //设置保存密码
        userEntity.setUserPassword(userPassword);
        //头像
        String bgImg = baseUserDemo.getPhoto();

        if (StringUtil.isEmpty(bgImg)) {
            //为空新增默认头像，后续添加
        } else {
            //上传头像
            String appIconPathBody = uploadUserHead(bgImg, userEntity.getUserId().trim());

            userEntity.setPhotoUrl(appIconPathBody);
        }
        userEntity.setGender(baseUserDemo.getGender());

        String searchKeyWord1 = PinYin.getPinYinHeadChar(baseUserDemo.getUserName().trim()).toUpperCase();
        //姓名简拼
        userEntity.setSearchKeyWord1(searchKeyWord1);
        //全拼
        String searchKeyWord2 = PinYin.getPingYin(baseUserDemo.getUserName().trim()).toUpperCase();
        userEntity.setSearchKeyWord2(searchKeyWord2);
        userEntity.setTel(baseUserDemo.getTel());
        userEntity.setMobile(baseUserDemo.getMobile());
        userEntity.setIdentificationTypeCode(baseUserDemo.getIdentificationTypeCode());
        userEntity.setIdentificationNo(baseUserDemo.getIdentificationNo());
        userEntity.setEmail(baseUserDemo.getEmail());
        userEntity.setWechat(baseUserDemo.getWechat());
        userEntity.setStatus(baseUserDemo.getStatus());
        userEntity.setIsCustomer(baseUserDemo.getIsCustomer());
        userEntity.setJobPosition(baseUserDemo.getJobPosition());
        userEntity.setUpdateTime(new Date());
        //保存人员中的部门信息
        if (false == StringUtil.isEmpty(orgList)) {
            StringBuffer dept = new StringBuffer();
            String[] orgCode = orgList.split(",");
            for (int i = 0; i < orgCode.length; i++) {
                TBaseOrg org = baseOrgMapper.selectByPrimaryKey(orgCode[i]);
                dept.append(",");
                dept.append(org.getOrgName());
            }
            //移动后的部门名称
            String deptStr = dept.substring(1);
            userEntity.setDept(deptStr);
        }
        userEntity.setIsAvailable(DbConstants.IS_AVAILABLE);

        baseUserMapper.insertSelective(userEntity);

        //组织
        if (false == StringUtil.isEmpty(orgList)) {
            String[] org = orgList.split(",");
            //删除人员与组织关系
            baseOrgUsersMapper.deletOrgUser(userEntity.getUserId());
            for (int i = 0; i < org.length; i++) {
                TBaseOrgUsers orgUser = new TBaseOrgUsers();
                orgUser.setOrgUserId(UUID.randomUUID().toString());
                orgUser.setOrgCode(org[i]);
                orgUser.setUserId(userEntity.getUserId());
                //取最大的排序号
                List<TBaseOrgUsers> reusltList = baseOrgUsersMapper.queryMaxNumber(org[i]);
                String orderNumber = null;
                if (reusltList != null && reusltList.size() > 0) {
                    orderNumber = reusltList.get(0).getUserOrderNumber();
                } else {
                    orderNumber = "0";
                }
                orgUser.setUserOrderNumber(String.valueOf(Integer.parseInt(orderNumber) + 1));
                baseOrgUsersMapper.insert(orgUser);
            }
        }
        //群组
        if (false == StringUtil.isEmpty(teamList)) {
            String[] team = teamList.split(",");
            //删除人员与群组的关系
            baseTeamUsersMapper.deleteTeamUser(userEntity.getUserId());
            for (int i = 0; i < team.length; i++) {
                TBaseTeamUsers teamUser = new TBaseTeamUsers();
                teamUser.setUserTeamId(UUID.randomUUID().toString());
                teamUser.setTeamCode(team[i]);
                teamUser.setUserId(userEntity.getUserId());
                baseTeamUsersMapper.insert(teamUser);
            }
        }
        //分类
        if (false == StringUtil.isEmpty(tagList)) {
            String[] tag = tagList.split(",");
            //删除分类与人员关系
            baseUserTagsMapper.deletTagUser(userEntity.getUserId());
            for (int i = 0; i < tag.length; i++) {
                TBaseUserTags userTag = new TBaseUserTags();
                userTag.setUserTagId(UUID.randomUUID().toString());
                userTag.setTagCode(tag[i]);
                userTag.setUserId(userEntity.getUserId());
                baseUserTagsMapper.insert(userTag);
            }
        }
        //角色
        if (StringUtils.isNotEmpty(roleList)) {
            String[] role = roleList.split(",");
            //删除角色与人员关系
            tBaseUserRolesMapper.deletUserRoleByUserId(userEntity.getUserId());
            for (int i = 0; i < role.length; i++) {
                TBaseUserRoles userRole = new TBaseUserRoles();
                userRole.setUserRoleId(UUID.randomUUID().toString());
                userRole.setUserId(userEntity.getUserId());
                userRole.setRoleCode(role[i]);
                tBaseUserRolesMapper.insert(userRole);
            }
        }
        return MessageCodeConstant.RTN_CODE_SUCCESS;

    }

    /**
     * Function Name               uploadUserHead
     *
     * @param bgImg
     * @param userCode
     * @return
     * @throws VanySoftBaseException
     * @description 图片上传
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月14日 下午5:32:51           SharpClaw_x                    Initial
     * *********************************************************************
     */
    public String uploadUserHead(String bgImg, String userCode) throws VanySoftBaseException {
        bgImg = bgImg.substring(4, bgImg.length() - 1);
        String appIconPathHead = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        String appIconPathBody = fileProperties.getUserHeadImgBasePath() + userCode + ".jpg";
        Base64ImageUtil.generateImage(bgImg, appIconPathHead + appIconPathBody);

        return appIconPathBody;
    }


    /**
     * Function Name               checkUsercode
     *
     * @return
     * @throws VanySoftBaseException
     * @description 检查UserCode是否重复
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月22日 上午10:29:27           SharpClaw_x                    Initial
     * *********************************************************************
     */
    public boolean checkUsercode(String userAccount) throws VanySoftBaseException {
        List<Map<String, Object>> userList = baseUserMapper.checkUsercode(userAccount);

        boolean chenck = true;
        if (userList != null && userList.size() > 0) {
            chenck = false;
        }
        return chenck;
    }

    /**
     * Function Name               checkUserAccount
     *
     * @param userAccount
     * @return
     * @description 通过userAccount检查唯一
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月29日 上午9:48:45           SharpClaw_x      Initial
     * *********************************************************************
     */
    public TBaseUser checkUserAccount2(String userAccount) {
        TBaseUser user = baseUserMapper.selectByUserAccount(userAccount);

        return user;
    }

    /**
     * Function Name               updatebaseUser
     *
     * @param orgList
     * @param teamList
     * @param tagList
     * @return
     * @throws VanySoftBaseException
     * @description 修改人员信息
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月22日 下午2:07:11           SharpClaw_x                    Initial
     * *********************************************************************
     */
    @Transactional
    public String updatebaseUser(BaseUserDemo baseUserDemo, String orgList, String teamList, String tagList, String roleList) throws VanySoftBaseException {
        if (StringUtils.isBlank(baseUserDemo.getUserId())) {
            throw new VanySoftBaseException(MessageCodeConstant.RTN_CODE_UNKNOW_ERROR, "userId is null");
        }
        TBaseUser user = (TBaseUser) baseUserMapper.selectByPrimaryKey(baseUserDemo.getUserId());
        user.setUserName(baseUserDemo.getUserName());
        //头像
        String bgImg = baseUserDemo.getPhoto();
        if (StringUtil.isEmpty(bgImg)) {
            //为空新增默认头像，后续添加
        } else {
            //上传头像
            String appIconPathBody = uploadUserHead(bgImg, user.getUserId());

            user.setPhotoUrl(appIconPathBody);
        }
        user.setGender(baseUserDemo.getGender());
        //姓名简拼
        String searchKeyWord1 = PinYin.getPinYinHeadChar(baseUserDemo.getUserName().trim()).toUpperCase();
        user.setSearchKeyWord1(searchKeyWord1);
        //全拼
        String searchKeyWord2 = PinYin.getPingYin(baseUserDemo.getUserName().trim()).toUpperCase();
        user.setSearchKeyWord2(searchKeyWord2);
        user.setTel(baseUserDemo.getTel());
        user.setMobile(baseUserDemo.getMobile());
        user.setIdentificationTypeCode(baseUserDemo.getIdentificationTypeCode());
        user.setIdentificationNo(baseUserDemo.getIdentificationNo());
        user.setEmail(baseUserDemo.getEmail());
        user.setWechat(baseUserDemo.getWechat());
        user.setStatus(baseUserDemo.getStatus());
        user.setJobPosition(baseUserDemo.getJobPosition());
        user.setUpdateTime(new Date());
        //保存人员中的部门信息
        if (false == StringUtil.isEmpty(orgList)) {
            StringBuffer dept = new StringBuffer();
            String[] orgCode = orgList.split(",");
            for (int i = 0; i < orgCode.length; i++) {
                TBaseOrg org = baseOrgMapper.selectByPrimaryKey(orgCode[i]);
                dept.append(",");
                dept.append(org.getOrgName());
            }
            //移动后的部门名称
            String deptStr = dept.substring(1);
            user.setDept(deptStr);
        }

        baseUserMapper.updateByPrimaryKeySelective(user);
        //组织
        if (false == StringUtil.isEmpty(orgList)) {
            String[] org = orgList.split(",");
            //删除人员与组织关系
            baseOrgUsersMapper.deletOrgUser(user.getUserId());
            //组织
            for (int i = 0; i < org.length; i++) {
                TBaseOrgUsers orgUser = new TBaseOrgUsers();
                orgUser.setOrgUserId(UUID.randomUUID().toString());
                orgUser.setOrgCode(org[i]);
                orgUser.setUserId(user.getUserId());
                //取最大的排序号
                List<TBaseOrgUsers> reusltList = baseOrgUsersMapper.queryMaxNumber(org[i]);
                String orderNumber = null;
                if (reusltList != null && reusltList.size() > 0) {
                    orderNumber = reusltList.get(0).getUserOrderNumber();
                } else {
                    orderNumber = "0";
                }
                orgUser.setUserOrderNumber(String.valueOf(Integer.parseInt(orderNumber) + 1));
                baseOrgUsersMapper.insert(orgUser);
            }
        }
        //群组
        if (!StringUtil.isEmpty(teamList)) {
            String[] team = teamList.split(",");
            //删除人员与群组的关系
            baseTeamUsersMapper.deleteTeamUser(user.getUserId());
            for (int i = 0; i < team.length; i++) {
                TBaseTeamUsers teamUser = new TBaseTeamUsers();
                teamUser.setUserTeamId(UUID.randomUUID().toString());
                teamUser.setTeamCode(team[i]);
                teamUser.setUserId(user.getUserId());
                baseTeamUsersMapper.insert(teamUser);
            }
        }
        //分类
        if (!StringUtil.isEmpty(tagList)) {
            String[] tag = tagList.split(",");
            //删除分类与人员关系
            baseUserTagsMapper.deletTagUser(user.getUserId());
            for (int i = 0; i < tag.length; i++) {
                TBaseUserTags userTag = new TBaseUserTags();
                userTag.setUserTagId(UUID.randomUUID().toString());
                userTag.setTagCode(tag[i]);
                userTag.setUserId(user.getUserId());
                baseUserTagsMapper.insert(userTag);
            }
        }
        //角色
        if (StringUtils.isNotEmpty(roleList)) {
            String[] role = roleList.split(",");
            //删除角色与人员关系
            tBaseUserRolesMapper.deletUserRoleByUserId(user.getUserId());
            for (int i = 0; i < role.length; i++) {
                TBaseUserRoles userRole = new TBaseUserRoles();
                userRole.setUserRoleId(UUID.randomUUID().toString());
                userRole.setUserId(user.getUserId());
                userRole.setRoleCode(role[i]);
                tBaseUserRolesMapper.insert(userRole);
            }
        }

        return MessageCodeConstant.RTN_CODE_SUCCESS;
    }

    /**
     * Function Name               deletebaseUser
     *
     * @return
     * @throws VanySoftBaseException
     * @description 批量删除人员
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月22日 下午2:50:14           SharpClaw_x                    Initial
     * *********************************************************************
     */
    @Transactional
    public String deletebaseUser(String userIdList) throws VanySoftBaseException {
        if (StringUtil.isEmpty(userIdList)) {
            throw new VanySoftBaseException(MessageCodeConstant.RTN_CODE_UNKNOW_ERROR, "userIdList is null");
        }

        String[] userCode = userIdList.split(",");

        for (int i = 0; i < userCode.length; i++) {
            //部门
            baseOrgUsersMapper.deletOrgUser(userCode[i]);
            //群组
            baseTeamUsersMapper.deleteTeamUser(userCode[i]);
            //分类
            baseUserTagsMapper.deletTagUser(userCode[i]);
            //删除角色与人员关系
            tBaseUserRolesMapper.deletUserRoleByUserId(userCode[i]);
            //人员
//			TBaseUser user=(TBaseUser) baseUserMapper.selectByPrimaryKey(userCode[i]);
//			user.setIsAvailable(DbConstants.IS_NOT_AVAILABLE);
            baseUserMapper.deleteByPrimaryKey(userCode[i]);
        }
        return MessageCodeConstant.RTN_CODE_SUCCESS;
    }

    /**
     * Function Name               updateOrderNumBaseUser
     *
     * @param action
     * @return
     * @throws VanySoftBaseException
     * @description 人员排序
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月22日 下午7:03:10           SharpClaw_x                    Initial
     * *********************************************************************
     */
    @Transactional
    public String updateOrderNumBaseUser(String orgCode, String userId, String action) throws VanySoftBaseException {
        //部门人员排序
        List<TBaseOrgUsers> reusltList = baseOrgUsersMapper.queryMaxNumber(orgCode);
        //获取到人员所在的list位置
        int order = 0;
        if (reusltList != null && reusltList.size() > 0) {
            for (int i = 0; i < reusltList.size(); i++) {
                if (userId.equals(reusltList.get(i).getUserId())) {
                    order = i;
                    break;
                }
            }
        }
        String temp;
        String befoeNumber;
        String number;
        String orgUserId = reusltList.get(order).getOrgUserId();
        //action up：上移;down：下移;top：置顶;bottom：置底
        if ("up".equals(action)) {
            befoeNumber = reusltList.get(order + 1).getUserOrderNumber();
            number = reusltList.get(order).getUserOrderNumber();

            temp = befoeNumber;
            befoeNumber = number;
            number = temp;
            //更新当前人的排序
            updateOrgUserNumber(orgUserId, number);
            //更新前一个人的排序
            String befoOrgUserId = reusltList.get(order + 1).getOrgUserId();
            updateOrgUserNumber(befoOrgUserId, befoeNumber);
        }
        if ("down".equals(action)) {
            if (order != 0) {
                befoeNumber = reusltList.get(order - 1).getUserOrderNumber();
                number = reusltList.get(order).getUserOrderNumber();
                temp = befoeNumber;
                befoeNumber = number;
                number = temp;

                //更新当前人的排序
                updateOrgUserNumber(orgUserId, number);
                //更新后一个人的排序
                String befoOrgUserId = reusltList.get(order - 1).getOrgUserId();
                updateOrgUserNumber(befoOrgUserId, befoeNumber);
            }
        }
        if ("top".equals(action)) {
            String bootm = reusltList.get(reusltList.size() - 1).getUserOrderNumber();
            //更新当前人的排序
            updateOrgUserNumber(orgUserId, String.valueOf(Integer.parseInt(bootm) - 1));
        }

        if ("bottom".equals(action)) {
            String bootm = reusltList.get(0).getUserOrderNumber();
            //更新当前人的排序
            updateOrgUserNumber(orgUserId, String.valueOf(Integer.parseInt(bootm) + 1));
        }

        return MessageCodeConstant.RTN_CODE_SUCCESS;
    }

    private void updateOrgUserNumber(String orgUserId, String number) {
        TBaseOrgUsers orgUser = baseOrgUsersMapper.selectByPrimaryKey(orgUserId);
        orgUser.setUserOrderNumber(number);
        baseOrgUsersMapper.updateByPrimaryKeySelective(orgUser);
    }

    /**
     * Function Name               resetUserPasswordBaseUser
     *
     * @param userId        被重置密码的用户
     * @param loginUserId   操作员
     * @param loginPassword 操作员密码
     * @return 重置的密码
     * @throws VanySoftBaseException
     * @description 重置密码
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月23日 上午9:31:07           SharpClaw_x                    Initial
     * 2017年7月7日 上午9:31:07          SharpClaw_x                    接口调整
     * *********************************************************************
     */
    @Transactional
    public Map<String, String> resetUserPasswordBaseUser(String userId, String loginUserId, String loginPassword) throws VanySoftBaseException {
        TBaseUser user = baseUserMapper.selectByPrimaryKey(loginUserId);
        String newPwd = "";
        String userAccount = "";
        if (user.getUserPassword().equals(DescUtil.encrypt(loginPassword))) { // 验证操作员的密码手正确
            TBaseUser baseUser = baseUserMapper.selectByPrimaryKey(userId);
            userAccount = baseUser.getUserAccount();//账号
            newPwd = DbConstants.INIT_PASSWORD + baseUser.getUserAccount() + "_" + baseUser.getUserAccount().length();// 新的密码
            String userpassword = DescUtil.encrypt(newPwd);
            baseUser.setUserPassword(userpassword);
            baseUserMapper.updateByPrimaryKeySelective(baseUser);
        } else {
            throw new VanySoftBaseException(MessageCodeConstant.UserAndRoleException.RTN_CODE_USERPASSWORD_CODE);
        }
        Map<String, String> map = new HashMap<>();
        map.put("userAccount", userAccount);
        map.put("userPassword", newPwd);
        return map;
    }

    /**
     * Function Name               resetUserStatus
     *
     * @param status
     * @return
     * @throws VanySoftBaseException
     * @description 人员禁用或启用
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月23日 上午10:23:32           SharpClaw_x                    Initial
     * *********************************************************************
     */
    @Transactional
    public String resetUserStatus(String userId, String status) throws VanySoftBaseException {

        TBaseUser baseUser = baseUserMapper.selectByPrimaryKey(userId);
        baseUser.setStatus(status);
        baseUserMapper.updateByPrimaryKeySelective(baseUser);
        return MessageCodeConstant.RTN_CODE_SUCCESS;
    }

    /**
     * Function Name               moveUsers
     *
     * @param orgCodeList
     * @return
     * @throws VanySoftBaseException
     * @description 人员移动
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月23日 下午1:53:45           SharpClaw_x                    Initial
     * *********************************************************************
     */
    @Transactional
    public String moveUsers(String userIdList, String orgCodeList) throws VanySoftBaseException {
//		if(StringUtil.isEmpty(orgUserIdList)){
//			throw new VanySoftBaseException(MessageCodeConstant.RTN_CODE_UNKNOW_ERROR,"orgUserIdList is null");
//		}
        //删除部门人员关系
//		String[] orgUserId=orgUserIdList.split(",");
//		for(int i=0;i<orgUserId.length;i++){
//			baseOrgUsersMapper.deleteByPrimaryKey(orgUserId[i]);
//		}
        if (StringUtil.isEmpty(orgCodeList)) {
            throw new VanySoftBaseException(MessageCodeConstant.RTN_CODE_UNKNOW_ERROR, "orgCodeList is null");
        }
        String[] orgCode = orgCodeList.split(",");
        StringBuffer dept = new StringBuffer();
        for (int i = 0; i < orgCode.length; i++) {
            TBaseOrg org = baseOrgMapper.selectByPrimaryKey(orgCode[i]);
            dept.append(",");
            dept.append(org.getOrgName());
        }
        //移动后的部门名称
        String deptStr = dept.substring(1);
        if (StringUtil.isEmpty(userIdList)) {
            throw new VanySoftBaseException(MessageCodeConstant.RTN_CODE_UNKNOW_ERROR, "userIdList is null");
        }
        String[] userCode = userIdList.split(",");
        for (int i = 0; i < userCode.length; i++) {
            //删除人员与组织关系
            baseOrgUsersMapper.deletOrgUser(userCode[i]);
            //更新人员中部门字段
            TBaseUser user = baseUserMapper.selectByPrimaryKey(userCode[i]);
            user.setDept(deptStr);
            baseUserMapper.updateByPrimaryKey(user);
        }
        for (int i = 0; i < orgCode.length; i++) {
            for (int j = 0; j < userCode.length; j++) {
                TBaseOrgUsers orgUser = new TBaseOrgUsers();
                orgUser.setOrgUserId(UUID.randomUUID().toString());
                orgUser.setOrgCode(orgCode[i]);
                orgUser.setUserId(userCode[j]);
                //取最大的排序号
                List<TBaseOrgUsers> reusltList = baseOrgUsersMapper.queryMaxNumber(orgCode[i]);
                String orderNumber = null;
                if (reusltList != null && reusltList.size() > 0) {
                    orderNumber = String.valueOf(reusltList.get(0).getUserOrderNumber());
                } else {
                    orderNumber = "0";
                }
                orgUser.setUserOrderNumber(String.valueOf(Integer.parseInt(orderNumber) + 1));
                baseOrgUsersMapper.insert(orgUser);
            }
        }

        return MessageCodeConstant.RTN_CODE_SUCCESS;
    }


    /**
     * Function Name               queryOrgInfoByUserCode
     *
     * @param userCode
     * @return
     * @description 根据用户代码查询组织机构信息
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月1日 下午10:26:00          SharpClaw_x                     Initial
     * *********************************************************************
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public List<Map<String, Object>> queryOrgInfoByUserCode(String userCode) {


        //查询当前登录用户拥有的组织机构数据,拼接成字符串
        String orgCodes = baseUserMapper.queryOrgCodesByUserCode(userCode);
        //根据组织机构字符串查询数据权限集
        List<Map> orgListMap = baseUserMapper.queryOrgListByOrgCodes(orgCodes);

        //将集合中的字符串集去重以逗号分割拼接成字符串
        String orgCodeList = CommonUtil.removeRepeatCode(orgListMap, "orgCodeList");


        //根据组织机构代码串查询组织组织机构树
        List<Map> orgMap = baseUserMapper.queryOrgInfoByOrgStr(orgCodeList);

        //根据用户代码查询显示用户的机构
        List<Map> showUserOrgListMap = baseUserMapper.queryOrgCodeShowUserCode(userCode);
        List<Map<String, Object>> mapList = new ArrayList();
        for (int i = 0; i < orgMap.size(); i++) {
            String orgCode = (String) orgMap.get(i).get("code");
            Map orgInfo = orgMap.get(i);
            //显示拥有管理权限的机构下用户信息
            for (int j = 0; j < showUserOrgListMap.size(); j++) {
                String getOrgCode = (String) showUserOrgListMap.get(j).get("orgCode");
                if (StringUtils.isEmpty(getOrgCode)) {
                    break;
                }
                String[] orgCodeGroup = getOrgCode.split(",");
                for (int k = 0; k < orgCodeGroup.length; k++) {
                    if (StringUtils.equals(orgCode, orgCodeGroup[k])) {
                        orgInfo.put("childrenList", baseUserMapper.queryUserInfoByOrgCode(orgCode));
                    }
                }
            }
            mapList.add(orgInfo);
        }
        List<Map<String, Object>> userOrgInfo = StringUtil.listToTree(mapList, "code", "parentOrgCode", "childrenList");

        return userOrgInfo;
    }


    /**
     * Function Name               loadRoleByUserCode
     *
     * @return
     * @description 根据所选用户代码查询所已分配角色或者未分配角色信息
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月2日 下午3:27:51          SharpClaw_x                     Initial
     * *********************************************************************
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public Map<String, Object> loadRoleByUserCode(String checkUserCode) {

        Map map = new HashMap();

        //未分配角色
        List<Map> NoAllotRoleMap = baseUserMapper.loadNoAllotRoleByUserCode(checkUserCode);

        //已分配角色
        List<Map> allotRoleMap = baseUserMapper.loadAllotRoleByUserCode(checkUserCode);

        map.put("noAllotRole", NoAllotRoleMap);
        map.put("allotRole", allotRoleMap);

        return map;
    }

    /**
     * Function Name               modifyUserRoleAllotAndRemove
     *
     * @param checkUserCode
     * @param roleCodes
     * @return
     * @description 所选用户的角色分配与移除
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月2日 下午5:15:26          SharpClaw_x                     Initial
     * *********************************************************************
     */
    @SuppressWarnings({"rawtypes"})
    @Transactional(propagation = Propagation.REQUIRED)
    public void modifyUserRoleAllotAndRemove(String checkUserCode, String roleCodes) {
        Map map = new HashMap<>();
        //根据用户代码删除用户所有拥有角色
        tBaseUserRolesMapper.deletUserRoleByUserId(checkUserCode);
        String[] roleCodeGroup = roleCodes.split(",");
        for (int i = 0; i < roleCodeGroup.length; i++) {
            TBaseUserRoles tBaseUserRoles = new TBaseUserRoles();
            tBaseUserRoles.setUserRoleId(UUID.randomUUID().toString());
            tBaseUserRoles.setUserId(checkUserCode);
            tBaseUserRoles.setRoleCode(roleCodeGroup[i]);
            tBaseUserRolesMapper.insert(tBaseUserRoles);
        }
    }


    /**
     * Function Name               loadFunctionByUserCode
     *
     * @param loginUserCode
     * @param checkUserCode
     * @return
     * @description 根据人员编号查询已分配功能权限和未分配功能权限
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月2日 下午7:04:06          SharpClaw_x                     Initial
     * *********************************************************************
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public Map<String, Object> loadFunctionByUserCode(String loginUserCode, String checkUserCode) {

        if (StringUtils.isEmpty(checkUserCode)) {
            checkUserCode = "";
        }

        Map resultMap = new HashMap<>();

        //查询已分配功能数据字符串
//		List<Map> allotFunctionStrs=tBaseUserFunctionsMapper.loadAllotFunctionStrsByUserCode(checkUserCode);
        //移除已分配功能重复字符串
//		String removeRepeatFunctionStrs=CommonUtil.removeRepeatCode(allotFunctionStrs, "functionCodes");
        //查询已分配功能信息,为展示成树结构数据,递归结果
//		List<Map> allotFunctions=baseFunctionMapper.queryFunctionInfoByCodesPart(removeRepeatFunctionStrs);


        //查询已分配功能信息,非递归结果
//		String allotFunction=baseFunctionMapper.queryFunctionCodeByUserCode(checkUserCode);

        //当前登录用户所拥有功能权限（非递归）
//		String loginUserFunctions=tBaseUserFunctionsMapper.loadFunctionByUserCodeUnion(loginUserCode);


        //去掉交集得未分配数据权限字符串(未分配=登录用户所拥有功能权限-所选用户已分配功能) 非递归
//		loginUserFunctions=noAllotStr(loginUserFunctions,allotFunction);


        //递归查询未分配功能集合代码
//		List<Map> functionList = tBaseUserFunctionsMapper.loadFunctionByCodes(loginUserFunctions);
        //移除未分配功能重复字符串
//		String noAllotFunctionStrs=CommonUtil.removeRepeatCode(functionList, "functionCodes");
        //查询出去重后树状结构的结果  (未分配功能=可分配功能-已分配功能)递归
//		List<Map> treeMapList=baseFunctionMapper.queryFunctionInfoByCodesPart(noAllotFunctionStrs);


        //根据功能字符串处理成树状结构图


        //已分配功能
//		List<Map<String,Object>> allotFunctionMapList=CommonUtil.getTreeDataList(allotFunctions, "functionCode", "parentFunctionCode");

        //未分配功能
//		List<Map<String,Object>> noAllotFunctionMapList=CommonUtil.getTreeDataList(treeMapList, "functionCode", "parentFunctionCode");


//		resultMap.put("noAllotFunction", noAllotFunctionMapList);
//		resultMap.put("allotFunction", allotFunctionMapList);
        return resultMap;

    }


    /**
     * Function Name               modifyUserRoleAllotAndRemove
     *
     * @return
     * @description 用户功能分配与移除
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月2日 下午5:15:26          SharpClaw_x                     Initial
     * *********************************************************************
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void modifyUserFunctionAllotAndRemove(String checkUserCode, String functionCodes) {


        //获取所选用户的数据权限
//		String managementScope=tBaseUserFunctionsMapper.queryManagementScopeByUserCode(checkUserCode);

//		if(StringUtils.isEmpty(managementScope)){
//			managementScope="";
//		}

        //更新
        //根据用户代码删除用户所有拥有功能
//		tBaseUserFunctionsMapper.deletUserFunctionByUserCode(checkUserCode);


//		if(StringUtils.isEmpty(functionCodes)){
        //传入功能代码字符串为空,则表示移除所有已分配功能
//			functionCodes="";
//			if(StringUtils.isNotEmpty(managementScope)){
        //如果数据权限范围不为空,则保留一条数据权限范围数据
//				TBaseUserFunctions tBaseUserFunctions=new TBaseUserFunctions();
//				tBaseUserFunctions.setUserFunctionId(UUID.randomUUID().toString());
//				tBaseUserFunctions.setUserId(checkUserCode);
//				tBaseUserFunctions.setManagementScope(managementScope);
//				tBaseUserFunctionsMapper.insert(tBaseUserFunctions);
//			}
//		}else{
        //分配与移除功能非空,则进行循环添加
//			String[] functionCodeGroup=functionCodes.split(",");
//			for(int i=0;i<functionCodeGroup.length;i++){
//				TBaseUserFunctions tBaseUserFunctions=new TBaseUserFunctions();
//				tBaseUserFunctions.setUserFunctionId(UUID.randomUUID().toString());
//				tBaseUserFunctions.setUserId(checkUserCode);
//				tBaseUserFunctions.setManagementScope(managementScope);
//				tBaseUserFunctions.setFunctionCode(functionCodeGroup[i]);
//				tBaseUserFunctionsMapper.insert(tBaseUserFunctions);
//			}
//		}
    }


    /**
     * Function Name               loadDataByUserCode
     *
     * @param loginUserCode
     * @param checkUserCode
     * @return
     * @description 根据人员编号查询已分配数据权限和未分配数据权限
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月5日 上午12:59:52          SharpClaw_x                     Initial
     * *********************************************************************
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public Map<String, Object> loadDataByUserCode(String loginUserCode, String checkUserCode) {

        if (StringUtils.isEmpty(checkUserCode)) {
            checkUserCode = "";
        }

        Map resultMap = new HashMap<>();

        //当前登录用户所拥有数据权限-递归
//		String loginUserManagementScope=tBaseUserFunctionsMapper.loadDataScopeByUserCodeUnion(loginUserCode);
        //查询选择用户已分配数据信息权限字符串-非递归
//		String allotDataScope=tBaseUserFunctionsMapper.loadDataScopeByUserCode(checkUserCode);
        //查询当前选择用户已分配数据信息权限-递归
//		List<Map> recursionAllotDataCode=tBaseUserFunctionsMapper.queryRecursionDataByCodes(allotDataScope);
        //查询当前已选择用户已分配数据权限-去重
//		String allotDataStr=CommonUtil.removeRepeatCode(recursionAllotDataCode, "orgCodeList");
//		List<Map> allotData=tBaseUserFunctionsMapper.loadAllotDataByUserCode(allotDataStr);


        //去重当前登录用户拥有数据权限
//		loginUserManagementScope=CommonUtil.removeRepeatStr(loginUserManagementScope);


        //去掉交集得未分配数据权限字符串(未分配=可分配-已分配)
//		loginUserManagementScope=noAllotStr(loginUserManagementScope,allotDataScope);
        //递归查询未分配数据代码集合
//		List<Map> recursionNoAllotDataCode=tBaseUserFunctionsMapper.queryRecursionDataByCodes(loginUserManagementScope);
        //未分配数据代码集合去重-递归(未分配数据权限字符串)
//		String noAllotDataScope=CommonUtil.removeRepeatCode(recursionNoAllotDataCode, "orgCodeList");
        //递归查询出来的未分配数据权限树集合  (未分配=可分配-已分配)
//		List<Map> noAllotData=tBaseUserFunctionsMapper.queryDataByCodesPart(noAllotDataScope);


        //根据数据字符串处理成树状结构图
        //已分配数据
//		List<Map<String,Object>> allotDataMapList=CommonUtil.getTreeDataList(allotData, "orgCode", "parentOrgCode");

        //未分配数据
//		List<Map<String,Object>> noAllotDataMapList=CommonUtil.getTreeDataList(noAllotData, "orgCode", "parentOrgCode");


//		resultMap.put("noAllotData", noAllotDataMapList);
//		resultMap.put("allotData", allotDataMapList);

        return resultMap;
    }

    /**
     * Function Name               modifyUserDataAllotAndRemove
     *
     * @param orgCodes
     * @return
     * @description 用户数据权限分配与移除
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月5日 下午3:02:42          SharpClaw_x                     Initial
     * *********************************************************************
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void modifyUserDataAllotAndRemove(String orgCodes, String checkUserCode) {

        if (StringUtils.isEmpty(orgCodes)) {
            orgCodes = "";
        }

//		int userFunctionCount=tBaseUserFunctionsMapper.queryCountUserFunctionByUserCode(checkUserCode);
        //判断是否有数据权限
//		if(userFunctionCount==0){
//			//如果无数据权限则新增
//			TBaseUserFunctions tBaseUserFunctions=new TBaseUserFunctions();
//			tBaseUserFunctions.setUserFunctionId(UUID.randomUUID().toString());
//			tBaseUserFunctions.setUserId(checkUserCode);
//			tBaseUserFunctions.setManagementScope(orgCodes);
//			tBaseUserFunctionsMapper.insert(tBaseUserFunctions);
//		}
        else {
            //根据数据权限范围更新数据权限
//			Map<String,Object> paramsMap =new HashMap<>();
//			paramsMap.put("dataScopeCodes", orgCodes);
//			paramsMap.put("userCode", checkUserCode);
//			tBaseUserFunctionsMapper.modifyUserDataScopeByUserCode(paramsMap);
        }
    }

    /**
     * Function Name               compareStrRemoveRepeat
     *
     * @return
     * @description 待分配权限=登录用户拥有权限-已分配权限
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月6日 下午7:25:05          SharpClaw_x                     Initial
     * *********************************************************************
     */
    public String noAllotStr(String loginUserStr, String allotStr) {
        if (StringUtils.isEmpty(loginUserStr)) {
            loginUserStr = "";
        }

        if (StringUtils.isNotEmpty(allotStr)) {
            //查询未分配功能信息
            String[] allotStrGroup = allotStr.split(",");
            for (int i = 0; i < allotStrGroup.length; i++) {
                String allotDataCode = allotStrGroup[i];
                //已分配功能是否存在当前登录拥有功能中,不存在则不移除
                int functionIndex = loginUserStr.indexOf(allotDataCode);
                if (functionIndex != -1) {
                    //如果功能不止一个时
                    if (loginUserStr.length() != allotDataCode.length()) {

                        //如果功能索引为0,则移除功能及逗号,逗号在后   例 (4e3c1a66-d0fc-4061-b201-c088942674cd,4e3c1a66-d0fc-4061-b201-c088942674cd)
                        if (functionIndex == 0) {
                            loginUserStr = loginUserStr.replace(allotDataCode + ",", "");
                        } else {
                            loginUserStr = loginUserStr.replace("," + allotDataCode, "");
                        }
                    } else {
                        //如果仅有一个功能时,消除后即无可分配功能
                        loginUserStr = "";
                        break;
                    }

                }
            }
        }
        return loginUserStr;
    }

    /**
     * Function Name               queryExcelOrgUser
     *
     * @param orgCode
     * @return
     * @description Excel查询部门下的人员，包括级联
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月16日 上午11:12:30           SharpClaw_x      Initial
     * *********************************************************************
     */
    public UserExport queryExcelOrgUser(String orgCode, String search) {
        //获取当前部门下我的权限下的组织集合
        String orgCodes = baseOrgMapper.queryOrgChildren(orgCode);

        if (StringUtils.isEmpty(search)) {
            search = null;
        } else {
            search = StringUtils.upperCase(search);
        }

        List<LoginUserinfo> userList = baseOrgMapper.queryOrgUsers(orgCodes, search, null);
        TBaseOrg org = baseOrgMapper.selectByPrimaryKey(orgCode);

        UserExport export = new UserExport();
        export.setFileName(org.getOrgName());
        export.setUsers(userList.stream().map(u -> JSONObject.parseObject(JSONObject.toJSONString(u))).collect(Collectors.toList()));

        return export;
    }

    /**
     * Function Name               addExcelUser
     *
     * @throws VanySoftBaseException
     * @description 添加Excel中的人员
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月16日 上午9:31:36           SharpClaw_x      Initial
     * *********************************************************************
     */
    @Transactional
    public List<ExcelOrgUserDemo> addExcelUser(List<ExcelUserDemo> users, String repeatDeal) throws VanySoftBaseException {

        List<ExcelOrgUserDemo> userOrgs = new ArrayList<ExcelOrgUserDemo>();

        for (ExcelUserDemo user : users) {
            TBaseUser userEntity = new TBaseUser();

            userEntity.setUserId(UUID.randomUUID().toString());
            userEntity.setUserAccount(user.getUserCode());
            userEntity.setUserName(user.getUserName());
            //设置保存密码
            userEntity.setUserPassword(DescUtil.encrypt(DbConstants.INIT_PASSWORD + user.getUserCode() + "_" + user.getUserCode().length()));

            userEntity.setGender(user.getGender());
            //姓名简拼
            userEntity.setSearchKeyWord1(PinYin.getPinYinHeadChar(user.getUserName()).toUpperCase());
            //全拼
            userEntity.setSearchKeyWord2(PinYin.getPingYin(user.getUserName()).toUpperCase());
            userEntity.setMobile(user.getMobile());
            userEntity.setEmail(user.getEmail());
            userEntity.setWechat(user.getWechat());
            userEntity.setJobPosition(user.getJobPostion());
            userEntity.setDept(user.getDept());
            userEntity.setStatus(DbConstants.IS_AVAILABLE);
            //和导入外部人员复用、customer字段由Action定制
            userEntity.setIsCustomer(user.getIsCustomer());
            userEntity.setUpdateTime(new Date());
            userEntity.setIsAvailable(DbConstants.IS_AVAILABLE);
            //检查userCode是否重复
            TBaseUser userDB = baseUserMapper.selectByUserAccount(userEntity.getUserAccount());
            if (userDB == null) {
                baseUserMapper.insertSelective(userEntity);

                //添加新的部门关系
                ExcelOrgUserDemo orgUser = new ExcelOrgUserDemo();
                orgUser.setUserId(userEntity.getUserId());
                orgUser.setOrgCodes(user.getOrgCodes());
                userOrgs.add(orgUser);
            } else {
                //userCode如果重复则判断处理条件
                //忽略不做操作
                if (StringUtils.equals(repeatDeal, DbConstants.USER_COVER)) {
                    //覆盖则更新
                    userEntity.setUserId(userDB.getUserId());
                    baseUserMapper.updateByPrimaryKey(userEntity);

                    //删除原有的群组，类别，角色关系？
                    baseTeamUsersMapper.deleteTeamUser(userDB.getUserId());
                    baseUserTagsMapper.deletTagUser(userDB.getUserId());
                    tBaseUserRolesMapper.deletUserRoleByUserId(userDB.getUserId());
                    //添加新的部门关系
                    ExcelOrgUserDemo orgUser = new ExcelOrgUserDemo();
                    orgUser.setUserId(userEntity.getUserId());
                    orgUser.setOrgCodes(user.getOrgCodes());
                    userOrgs.add(orgUser);
                }

            }
        }

        return userOrgs;

    }


    /**
     * Function Name               dealUserOrg
     *
     * @description 处理用户的机构
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月16日 上午9:48:39           SharpClaw_x      Initial
     * *********************************************************************
     */
    @Transactional
    public void addOrgUsers(List<ExcelOrgUserDemo> list) {

        for (ExcelOrgUserDemo demo : list) {
            //组织
            String userOrgCodes = demo.getOrgCodes();
            String userCode = demo.getUserId();

            if (false == StringUtil.isEmpty(userOrgCodes)) {
                String[] orgArr = userOrgCodes.split(",");
                //删除人员与组织关系
                baseOrgUsersMapper.deletOrgUser(userCode);
                for (String org : orgArr) {
                    TBaseOrgUsers orgUser = new TBaseOrgUsers();
                    orgUser.setOrgUserId(UUID.randomUUID().toString());
                    orgUser.setOrgCode(org);
                    orgUser.setUserId(userCode);
                    //取最大的排序号
                    List<TBaseOrgUsers> reusltList = baseOrgUsersMapper.queryMaxNumber(org);
                    String orderNumber = null;
                    if (reusltList != null && reusltList.size() > 0) {
                        orderNumber = String.valueOf(reusltList.get(0).getUserOrderNumber());
                    } else {
                        orderNumber = "0";
                    }
                    orgUser.setUserOrderNumber(String.valueOf(Integer.parseInt(orderNumber) + 1));
                    baseOrgUsersMapper.insert(orgUser);
                }
            }
        }
    }

    /**
     * Function Name               getBaseUserInfo
     *
     * @param loginUserId
     * @param code
     * @param gender
     * @param orgCode
     * @param teamCode
     * @param tagCode
     * @return
     * @description 人员筛选器
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月20日 下午1:55:49           SharpClaw_x                    Initial
     * *********************************************************************
     */
    public Page<TBaseUser> getBaseUserInfo(String loginUserId, String code, String gender, String orgCode,
                                           String teamCode, String tagCode, Integer pageIndex, Integer pageSize) {

        // 查询当前人的数据范围
        String allOrgCodes = queryMyAllOrg(loginUserId);

        Page<TBaseUser> page = new Page<>(pageIndex, pageSize);
        List<TBaseUser> resultList = baseUserMapper.getBaseUserInfoBycondition(page, allOrgCodes, code, gender, orgCode, teamCode, tagCode);
        page.setRecords(resultList);
        return page;
    }

    /**
     * Function Name               queryMyAllOrg
     *
     * @param userId
     * @return
     * @description 查询我的权限下的部以及所有的子部门
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月26日 下午3:02:18           SharpClaw_x      Initial
     * *********************************************************************
     */
    public String queryMyAllOrg(String userId) {
        int adminFlag = loginMapper.checkAdminByUserId(userId);
        String myOrgCodes = null;
        if (adminFlag == 0) {
            myOrgCodes = baseUserMapper.queryDataManagement(userId);
//			//查询角色已分配数据信息集合-递归
//			List<Map> allotDataList=baseRoleMapper.queryRecursionDataByCodes(myOrgCodes);
//			//去重
//			myOrgCodes=CommonUtil.removeRepeatCode(allotDataList, "orgCodeList");
        } else {
            myOrgCodes = tBaseOrgMapper.queryAdminOrgCodes();
        }

        String[] orgCodeArr = myOrgCodes.split(",");

        String myChildrenCodes = "";
        for (String orgCode : orgCodeArr) {
            myChildrenCodes += "," + tBaseOrgMapper.queryOrgChildren(orgCode);
        }

        myChildrenCodes = myChildrenCodes.substring(1);

        return myChildrenCodes;
    }

    /**
     * Function Name               getBaseUserAllInfo
     *
     * @param code
     * @param gender
     * @param orgCode
     * @param teamCode
     * @param tagCode
     * @param pageIndex
     * @param pageSize
     * @return
     * @description (发送消息查询所有人员 - 消息中心)
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年8月29日 下午2:08:25          SharpClaw_x                     Initial
     * *********************************************************************
     */
    public Page<Map<String, Object>> getBaseUserAllInfo(String code, String gender, String orgCode,
                                                        String teamCode, String tagCode, Integer pageIndex, Integer pageSize) {

        Page<Map<String, Object>> page = new Page<>(pageIndex, pageSize);
        List<Map<String, Object>> resultList = baseUserMapper.getBaseUserAllInfoBycondition(page, code, gender, orgCode, teamCode, tagCode);
        page.setRecords(resultList);
        return page;
    }

}
